package com.hustzp.com.xichuangzhu.utils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ScrollView;
import android.widget.Toast;

import com.hustzp.com.xichuangzhu.R;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 截图操作
 * Created by Shall on 2015-07-22.
 */
public class ScreenshotUtil {

    private final static String FILE_SAVEPATH = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "xichuangzhu" + File.separator;

    /**
     * 因为课表是可以滑动 的所以截取
     * 截取scrollview的屏幕
     **/
    public static String saveBitmapView(Context mContext, final View view, boolean isAdd) {
        int h = 0;
        if (view instanceof ScrollView) {
            ScrollView mScrollView = (ScrollView) view;
            for (int i = 0; i < mScrollView.getChildCount(); i++) {
                h += mScrollView.getChildAt(i).getHeight();
                mScrollView.getChildAt(i).setBackgroundColor(
                        Color.parseColor("#ffffff"));
            }
        } else {
            h = view.getHeight();
        }

        // 创建对应大小的bitmap
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        view.draw(canvas);

        // 添加水印
        Bitmap nbitmap = bitmap;
        if (isAdd) {
            nbitmap = Bitmap.createBitmap(view.getWidth(), h, Bitmap.Config.ARGB_8888);
            Canvas ncanvas = new Canvas(nbitmap);
            ncanvas.drawBitmap(bitmap, 0, 0, null);

            Bitmap ibitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_xcz_logo);
            ncanvas.drawBitmap(ibitmap, nbitmap.getWidth() - ibitmap.getWidth() - 10, nbitmap.getHeight() - ibitmap.getHeight() - 10, null);
            ncanvas.save(Canvas.ALL_SAVE_FLAG);// 保存
            // store
            ncanvas.restore();
        }

        File savedir = new File(FILE_SAVEPATH);
        if (!savedir.exists()) {
            savedir.mkdirs();
        }

        FileOutputStream out = null;
        String fileName = "xichuangzhu" + new SimpleDateFormat("yyyyMMddHHmmssS").format(new Date()) + ".png";
        String filePath = FILE_SAVEPATH + File.separator + fileName;
        try {
            out = new FileOutputStream(filePath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(mContext, "保存失败", Toast.LENGTH_SHORT).show();
        }
        try {
            if (null != out) {
                nbitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                out.flush();
                out.close();
            }
            Toast.makeText(mContext, "保存成功", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            Toast.makeText(mContext, "保存失败", Toast.LENGTH_SHORT).show();
        }

        // 其次把文件插入到系统图库
        try {
            MediaStore.Images.Media.insertImage(mContext.getContentResolver(),
                    filePath, fileName, null);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        // 最后通知图库更新
        mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + filePath)));
        return filePath;
    }

    /**
     * 合并图片
     *
     * @param head
     * @param kebiao
     * @param san
     * @return
     */
    public static Bitmap toConformBitmap(Bitmap head, Bitmap kebiao, Bitmap san) {
        if (head == null) {
            return null;
        }
        int headWidth = head.getWidth();
        int kebianwidth = kebiao.getWidth();
        int fotwid = san.getWidth();

        int headHeight = head.getHeight();
        int kebiaoheight = kebiao.getHeight();
        int footerheight = san.getHeight();
        //生成三个图片合并大小的Bitmap
        Bitmap newbmp = Bitmap.createBitmap(kebianwidth, headHeight + kebiaoheight + footerheight, Bitmap.Config.ARGB_8888);
        Canvas cv = new Canvas(newbmp);
        cv.drawBitmap(head, 0, 0, null);// 在 0，0坐标开始画入headBitmap

        //因为手机不同图片的大小的可能小了 就绘制白色的界面填充剩下的界面
        if (headWidth < kebianwidth) {
            System.out.println("绘制头");
            Bitmap ne = Bitmap.createBitmap(kebianwidth - headWidth, headHeight, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(ne);
            canvas.drawColor(Color.WHITE);
            cv.drawBitmap(ne, headWidth, 0, null);
        }
        cv.drawBitmap(kebiao, 0, headHeight, null);// 在 0，headHeight坐标开始填充课表的Bitmap
        cv.drawBitmap(san, 0, headHeight + kebiaoheight, null);// 在 0，headHeight + kebiaoheight坐标开始填充课表的Bitmap
        //因为手机不同图片的大小的可能小了 就绘制白色的界面填充剩下的界面
        if (fotwid < kebianwidth) {
            System.out.println("绘制");
            Bitmap ne = Bitmap.createBitmap(kebianwidth - fotwid, footerheight, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(ne);
            canvas.drawColor(Color.WHITE);
            cv.drawBitmap(ne, fotwid, headHeight + kebiaoheight, null);
        }
        cv.save(Canvas.ALL_SAVE_FLAG);// 保存
        cv.restore();// 存储
        //回收
        head.recycle();
        kebiao.recycle();
        san.recycle();
        return newbmp;
    }


}
